package com.kunbo.cn.service.impl.core.approval;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import com.kunbo.cn.dao.core.approval.TareExceptionApprovalDao;
import com.kunbo.cn.entity.core.approval.TareExceptionApproval;
import com.kunbo.cn.service.core.approval.TareExceptionApprovalService;
import com.kunbo.cn.util.date.SDate;
import com.kunbo.cn.util.retu.CommonResult;
import com.kunbo.cn.util.retu.Pages;
@Service
public class TareExceptionApprovalServiceImpl implements TareExceptionApprovalService {
	@Autowired
	private TareExceptionApprovalDao dao;
	@Autowired
	private JdbcTemplate jdbc;
	
	@Override
	public void save(TareExceptionApproval tareApproval) {
		//tareApproval.setAuditor_time(SDate.getSystemDateToString());
		dao.save(tareApproval);
	}

	@Override
	public int updateState(String id ,String state,String note,String auditor) {
		String sql = "UPDATE \r\n" +
		        "business_tare_exception_approval_manage  \r\n"+
		        " set state= '"+state+"'\r\n "+
		        " set note= '"+note+"'\r\n "+
		        " set auditor= '"+auditor+"'\r\n "+
		        " WHERE  id='"+id+"'";
				 int i=jdbc.update(sql);
				return i;
	}

	@Override
	public void delete(String id) {
		dao.delete(id);
	}

	@Override
	public TareExceptionApproval findById(String id) {
		return dao.findOne(id);
	}

	@Override
	public CommonResult list (String startTime, String endTime,String car_number,String material,String state, Integer page, Integer pageSize) {
		// TODO Auto-generated method stub
		CommonResult result = new CommonResult();
		String sql = "select * from business_tare_exception_approval_manage where 1=1 ";
		if (startTime != null && !startTime.equals("")) {
			sql += " and auditor_time >='" + startTime +"'" ;
		}
		if (endTime != null && !endTime.equals("")) {
			sql += " and auditor_time <='" + endTime +"'" ;
		}
		if (car_number != null && !car_number.equals("")) {
			sql += " and car_number like'" + car_number +"'" ;
		}if (material != null && !material.equals("")) {
			sql += " and material  like'" + material +"'" ;
		}if (state != null && !state.equals("")) {
			sql += " and state ='" + state +"'" ;
		
		sql +=" order by id desc";
		System.out.println("打印sql:"+sql);
		List<Map<String,Object>> listCount = jdbc.queryForList(sql);
		sql +=" limit " + (page*pageSize) + "," + pageSize;
		List<Map<String,Object>> list = jdbc.queryForList(sql);
		result.setMsg("success");
		result.setObj(list);
		result.setCount(listCount.size());
		System.out.println("打印sql:"+sql);
		
	}
		return result;
	}
	public Pages exelist(String startTime, String endTime,String car_number,String material,String state, Integer page, Integer pageSize) {
		String sql1 = "select * from business_tare_exception_approval_manage where 1=1 ";
		if (startTime != null && !startTime.equals("")) {
			sql1 += " and auditor_time >='" + startTime +"'" ;
		}
		if (endTime != null && !endTime.equals("")) {
			sql1 += " and auditor_time <='" + endTime +"'" ;
		}
		if (car_number != null && !car_number.equals("")) {
			sql1 += " and car_number like'" + car_number +"'" ;
		}if (material != null && !material.equals("")) {
			sql1 += " and material  like'" + material +"'" ;
		}if (state != null && !state.equals("")) {
			sql1 += " and state ='" + state +"'" ;
 }
		String sql2="";
		sql2+= " order by create_time  desc  limit " + (page * pageSize) +"," + pageSize;
		System.out.println(sql1+"<!!!!!!!!!!!!!!!!!!!!");
		  String sql=sql1.concat(sql2);//获取数据sql
		List<Map<String, Object>> list = jdbc.queryForList(sql);
		Pages pages =new Pages();
		  Integer count = jdbc.queryForList(sql1).size();
		  pages.setTotal(count);
		  pages.setData(list);
		return pages;
}
}


